﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class hydevmanager_admin_manager_menu : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session["timeout"] == null)
            {
                Response.Redirect("login.aspx");
            }
            else
            {
                 load_data_to_grAllMenu();
            them_menu_btn();
            }
           
        }
    }

    protected void load_data_to_grAllMenu()
    {
        int idParent = Convert.ToInt32(Request.QueryString.Get("IdParent"));
        DataTable dtnews = DataAccess.ExeSQLToDataTable("Select * from dev_Menu where idparent=" + idParent +"");
        grAllMenu.DataSource = dtnews;
        grAllMenu.DataBind();
    }

    protected void grAllenu_click(object sender, CommandEventArgs e)
    {
        string Argvalue = e.CommandArgument.ToString();
        char[] ch = new char[] { '|' };

        string[] s = ITNLip.SplitString(Argvalue, ch);
        string id = s[0];
        string action = s[1];
        string value = s[2];

        switch (action)
        {
            case "down":
                int order_value = Convert.ToInt32(value);
                if (order_value > 0)
                {
                    DataAccess.ExeNonSQL("Update dev_Menu set menu_order=menu_order-1 Where IdMenu=" + id + "");
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Thông báo!", "alert('Giá trị hiện tại đã nhỏ nhất!')", true);
                }
                break;
            case "up":
                DataAccess.ExeNonSQL("Update dev_Menu set menu_order=menu_order+1 Where IdMenu=" + id + "");
                break;
            case "browrs":
                string status = "0";
                if (value == "False")
                {
                    status = "1";
                }
                DataAccess.ExeNonSQL("Update dev_Menu set Status='" + status + "' where IdMenu=" + id + "");
                break;
            case "delete":
                int IdParent = Convert.ToInt32(DataAccess.ExeSQLGetInt("Select IdParent from dev_Menu where IdMenu=" + id + ""));
                
                DataAccess.ExeNonSQL("Delete from dev_Menu where IdMenu=" + id + "");
                
                if (IdParent != 0)
                {
                    int CountChild = Convert.ToInt32(DataAccess.ExeSQLGetInt("Select Count(IdParent) from dev_Menu where IdParent=" + IdParent + ""));
                    if (CountChild == 0)
                    {
                        DataAccess.ExeNonSQL("Update dev_Menu set has_child=0 where IdMenu=" + IdParent + "");
                    }
                }

                break;
        }
        load_data_to_grAllMenu();
    }

    protected void hasChild(int IdMenu)
    {
        int CountChild = Convert.ToInt32(DataAccess.ExeSQLGetInt("Select Count(IdParent) from dev_Menu where IdParent=" + IdMenu + ""));
        if (CountChild == 0)
        {
            DataAccess.ExeNonSQL("Update dev_Menu set has_child=0 where IdMenu=" + IdMenu + "");
        }
    }

    protected void them_menu_btn()
    {
        string value = Request.QueryString.Get("IdParent");
        string IdParent = "";

        if (value == null) { IdParent = "0"; }
        else { IdParent = value; }

        string htmlCode = "<a href='admin_add_menu.aspx?IdMenu=" + IdParent + "' class='a_themMenu'><img src='style/cssTran/img/them_menu.png' /></a>";
        lt.Text = htmlCode;
    }
    //===============PUBLIC STATIC======================================================================
    #region Hàm Public interface
    public static string test_has_menu_child(bool value, int IdMenu)
    {
        string htmlCode = "<a><img alt='' title='Can not Add' src='style/cssTran/img/warning_16.png' /></a>";
        if (value == false)
        {
            htmlCode = "<a href='admin_add_menu.aspx?IdMenu=" + IdMenu + "'><img alt='' title='Click for Add SubMenu' src='style/cssTran/img/add_16.png' /></a>";
        }
        return htmlCode;
        //HttpContext.Current.Request.QueryString.Get("");
    }

    public static string test_infor_menu(bool value, int IdMenu)
    {
        string htmlCode = "<a><img alt='' title='No Information' src='style/cssTran/img/error_button_16.png' /></a>";
        if (value == true)
        {
            htmlCode = "<a href='admin_manager_menu.aspx?IdParent=" + IdMenu + "'><img alt='' title='Click for Information' src='style/cssTran/img/info_button_16.png' /></a>";
        }
        return htmlCode;
    }

    public static string test_browrs_menu(bool value)
    {
        string htmlCode = "menu_browrs_off";
        if (value == true)
        {
            htmlCode = "menu_browrs_on";
        }
        return htmlCode;
    }

    public static string test_cant_delete(bool value)
    {
        string htmlCode = "<img alt='' src='style/cssTran/img/close_16.png' title='Click to Delete' />";
        if (value==true)
        {
            htmlCode = "<img alt='' src='style/cssTran/img/close_off_16.png' title='Can not Delete' />";
        }

        return htmlCode;
    }

    public static bool bool_inverse(bool value)
    {
        bool set = false;
        if (value==false)
        {
            set = true;
        }
        return set;
    }
    #endregion
}
